/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.common.utils.exception;

import java.util.Date;
import java.util.List;

import com.google.common.collect.Lists;

/**
 * xxx
 * 
 * @author Wangyi
 * @version v1.0
 * 
 * @date 2013-8-28
 * 
 */
public class InterSystemExceptionSpec extends ExceptionSpec {

	private String system;
	private List<String> systemChains;

	/**
	 * 
	 */
	public InterSystemExceptionSpec() {

	}

	public InterSystemExceptionSpec(String system, String msg,
			Date exceptionTime) {
		super(msg, exceptionTime);
		this.system = system;
	}

	public InterSystemExceptionSpec(String system, String code, String msg,
			Date exceptionTime) {
		super(code, msg, exceptionTime);
		this.system = system;
	}

	/**
	 * @return the system
	 */
	public String getSystem() {
		return system;
	}

	/**
	 * @param system
	 *            the system to set
	 */
	public void setSystem(String system) {
		this.system = system;
	}

	/**
	 * @return the systemChains
	 */
	public List<String> getSystemChains() {
		return systemChains;
	}

	/**
	 * @param systemChains
	 *            the systemChains to set
	 */
	public void setSystemChains(List<String> systemChains) {
		this.systemChains = systemChains;
	}

	public InterSystemExceptionSpec putSystem(String system) {
		if (this.systemChains == null) {
			this.systemChains = Lists.newArrayList();
		}
		this.systemChains.add(system);
		return this;
	}

	public String[] getSystemInvokeSequence() {
		List<String> seqs = Lists.newArrayList(system);
		if (this.systemChains != null && !this.systemChains.isEmpty()) {
			for (int i = this.systemChains.size() - 1; i >= 0; i--) {
				seqs.add(this.systemChains.get(i));
			}
		}
		return seqs.toArray(new String[0]);
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(super.toString());
		sb.append(",invokeSequence:'");
		String[] seqs = getSystemInvokeSequence();
		if (seqs.length > 0) {
			sb.append(seqs[0]);
			for (int i = 1; i < seqs.length; i++) {
				sb.append("->").append(seqs[i]);
			}
			sb.append("'");
		} else {
			sb.append("none");
		}
		return sb.toString();
	}
}
